package com.qf.usermybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.usermybatis.dao.UserMapper;
import com.qf.usermybatis.entity.User;
import com.qf.usermybatis.service.UserService;
import com.qf.usermybatis.utils.MyBatisUtil;

import java.util.List;

/**
 * @author zed
 * @date 2022/4/24 15:33
 */
public class UserServiceImpl implements UserService {

    @Override
    public PageInfo<User> selectAll(Integer pageNum, Integer pageSize) {
        PageInfo<User> pageInfo = null;
        try {
            UserMapper userMapper = MyBatisUtil.getMapper(UserMapper.class);
            PageHelper.startPage(pageNum,pageSize);
            List<User> users = userMapper.selectAll();
            pageInfo = new PageInfo<>(users);
            MyBatisUtil.commit();
        }catch (Exception e){
            MyBatisUtil.rollback();
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public int addUsers(List<User> users) {
        int add = 0;
        try {
            UserMapper userMapper = MyBatisUtil.getMapper(UserMapper.class);
            add = userMapper.insertBatch(users);
            MyBatisUtil.commit();
        }catch (Exception e){
            MyBatisUtil.rollback();
            e.printStackTrace();
        }
        return add;
    }
}
